package com.sc.controller;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.sc.model.User;
import com.sc.service.UserService;


@Controller
@RequestMapping("/user")
public class UserController {
	
	@Autowired
	private UserService userService;
	
	
	@RequestMapping()
	public String loginIn(){
		return "user/login";
	}
	
	@RequestMapping("/registerIn")
	public String registerIn(){
		return "user/register";
	}
	
	//展示个人信息页面
	@RequestMapping("/message")
	public String getMessage(Model m,String name){
		User user=userService.findUserByName(name);
		m.addAttribute("user",user);
		return "user/message";
	}
	
	@RequestMapping("/myspace")
	public String showMySpace(){
		return "user/index";
	}
	
	//登录校验用户是否存在
	@RequestMapping("/checkName")
	public void checkUser(String name,HttpSession session,HttpServletResponse response) throws IOException{
		response.setCharacterEncoding("utf-8");
		PrintWriter out=response.getWriter();
		User user=userService.findUserByName(name);
		String str="";
		if(user==null){
			str="无此用户,请重新输入!";
		}
		out.write(str);
		out.flush();
		out.close();
	}
	
	
	//注册校验用户名是否被占用
	@RequestMapping("/checkName1")
	@ResponseBody
	public String check1(String name,String pwd,HttpSession session,HttpServletRequest request,HttpServletResponse response) throws IOException{
		User user=userService.findUserByName(name);
		String str="";
		if(user!=null){
			str="用户已被注册";
		}
		return str;
	}
	//登录
	@RequestMapping("/userLogin")
	public String login(String username,String password,HttpSession session,HttpServletRequest request){
		User user=userService.findUserByNamePwd(username, password);
		if(user!=null){
			session.setAttribute("user",user);
			session.setAttribute("userId",user.getId());
			return "redirect:/home.html";
		}else{
			request.setAttribute("mes","密码错误");
			return "user/login";
		}
		
	}
	

	
	//用户注册
	@RequestMapping("/register")
	public String register(User user,String password,HttpSession session,HttpServletRequest request,HttpServletResponse response){
		userService.insertUser(user);
		request.setAttribute("success", "注册成功");
		return "user/register";
	}
	
	//修改用户时查询
	@RequestMapping("/findUserByName")
	public String findUserByName(String name,HttpServletRequest request){
		User user=userService.findUserByName(name);
		request.setAttribute("user",user);
		return "user/updateUser";
	}
	
	
	//修改用户信息
	@RequestMapping("/updateUser")
	public String updateUser(User user,HttpServletRequest request){
		User user1=userService.findUserByName(user.getName());
		user.setId(user1.getId());
		user.setCreateDate(user1.getCreateDate());
		user.setBirthDate(user1.getBirthDate());
		userService.updateUser(user);
		request.setAttribute("mes","修改成功");
		return "user/message";
	}
	//退出
	@RequestMapping("/remove")
	public String loginOut(HttpServletRequest req){
		req.getSession().removeAttribute("user");
		return "redirect:/home.html";
	}
	
	
}
